const router = require('koa-router')()
const {getLease, getLeaseById, postLease, patchLease, deleteLease, getLeaseMy, getLeaseYours} = require('../../controller/lease')

router.prefix('/api/lease')

router.get('/lease', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getLease({status, createTime, pageIndex, pageSize})
})

router.get('/lease/my', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getLeaseMy(ctx, {status, createTime, pageIndex, pageSize})
})

router.get('/lease/yours', async (ctx, next) => {
  const {status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getLeaseYours(ctx, {status, createTime, pageIndex, pageSize})
})

router.get('/lease/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getLeaseById(id)
})

router.post('/lease', async (ctx, next) => {
  const {content, askId, status = 1} = ctx.request.body
  ctx.body = await postLease({content, askId, status})
})

router.patch('/lease', async (ctx, next) => {
  const {content, status, id} = ctx.request.body
  ctx.body = await patchLease({content, status, id})
})

router.patch('/lease/status', async (ctx, next) => {
  const {status, id, times} = ctx.request.body
  ctx.body = await patchLease({status, id, times})
})

router.delete('/lease/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteLease(id)
})

module.exports = router
